home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00036_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  5.0 KB  |  176 lines

  1. /*
  2.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  6.  * without fee is hereby granted.
  7.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  8.  * for further important copyright and trademark information and to
  9.  * http://java.sun.com/licensing.html for further important licensing
  10.  * information for the Java (tm) Technology.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  20.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  21.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  22.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  23.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  24.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  25.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  26.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  27.  * HIGH RISK ACTIVITIES.
  28.  */
  29.  
  30. // author: Rachel Gollub, 1995
  31. // Time!
  32.  
  33. import java.util.*;
  34. import java.awt.*;
  35. import java.applet.*;
  36.  
  37. public class Clock2 extends Applet implements Runnable {
  38.   Thread timer = null;
  39.   int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
  40.   Date dummy = new Date();
  41.   String lastdate = dummy.toLocaleString();
  42.   
  43. public void init() {
  44.   int x,y;
  45.   resize(300,300);              // Set clock window size
  46. }
  47.  
  48.   // Plotpoints allows calculation to only cover 45 degrees of the circle,
  49.   // and then mirror
  50.  
  51. public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  52.  
  53.   g.drawLine(x0+x,y0+y,x0+x,y0+y);
  54.   g.drawLine(x0+y,y0+x,x0+y,y0+x);
  55.   g.drawLine(x0+y,y0-x,x0+y,y0-x);
  56.   g.drawLine(x0+x,y0-y,x0+x,y0-y);
  57.   g.drawLine(x0-x,y0-y,x0-x,y0-y);
  58.   g.drawLine(x0-y,y0-x,x0-y,y0-x);
  59.   g.drawLine(x0-y,y0+x,x0-y,y0+x);
  60.   g.drawLine(x0-x,y0+y,x0-x,y0+y);
  61. }
  62.  
  63.   // Circle is just Bresenham's algorithm for a scan converted circle
  64.  
  65. public void circle(int x0, int y0, int r, Graphics g) {
  66.   int x,y;
  67.   float d;
  68.  
  69.   x=0;
  70.   y=r;
  71.   d=5/4-r;
  72.   plotpoints(x0,y0,x,y,g);
  73.  
  74.   while (y>x){
  75.     if (d<0) {
  76.       d=d+2*x+3;
  77.       x++;
  78.     }
  79.     else {
  80.       d=d+2*(x-y)+5;
  81.       x++;
  82.       y--;
  83.     }
  84.     plotpoints(x0,y0,x,y,g);
  85.   }
  86. }
  87.  
  88.  
  89.   // Paint is the main part of the program
  90.  
  91. public void paint(Graphics g) {
  92.   int xh, yh, xm, ym, xs, ys, s, m, h, xcenter, ycenter;
  93.   String today;
  94.   Date dat = new Date();
  95.  
  96.   s = dat.getSeconds();
  97.   m = dat.getMinutes();
  98.   h = dat.getHours();
  99.   today = dat.toLocaleString();
  100.   xcenter=80;
  101.   ycenter=55;
  102.   
  103.   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  104.   // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  105.   
  106.   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  107.   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  108.   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  109.   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  110.   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  111.   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  112.   
  113.   // Draw the circle and numbers
  114.   
  115.   g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
  116.   g.setColor(Color.blue);
  117.   circle(xcenter,ycenter,50,g);
  118.   g.setColor(Color.darkGray);
  119.   g.drawString("9",xcenter-45,ycenter+3); 
  120.   g.drawString("3",xcenter+40,ycenter+3);
  121.   g.drawString("12",xcenter-5,ycenter-37);
  122.   g.drawString("6",xcenter-3,ycenter+45);
  123.  
  124.   // Erase if necessary, and redraw
  125.   
  126.   g.setColor(Color.lightGray);
  127.   if (xs != lastxs || ys != lastys) {
  128.     g.drawLine(xcenter, ycenter, lastxs, lastys);
  129.     g.drawString(lastdate, 5, 125);
  130.   }
  131.   if (xm != lastxm || ym != lastym) {
  132.     g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  133.     g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  134.   if (xh != lastxh || yh != lastyh) {
  135.     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  136.     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  137.   g.setColor(Color.darkGray);
  138.   g.drawString(today, 5, 125);  
  139.   g.drawLine(xcenter, ycenter, xs, ys);
  140.   g.setColor(Color.blue);
  141.   g.drawLine(xcenter, ycenter-1, xm, ym);
  142.   g.drawLine(xcenter-1, ycenter, xm, ym);
  143.   g.drawLine(xcenter, ycenter-1, xh, yh);
  144.   g.drawLine(xcenter-1, ycenter, xh, yh);
  145.   lastxs=xs; lastys=ys;
  146.   lastxm=xm; lastym=ym;
  147.   lastxh=xh; lastyh=yh;
  148.   lastdate = today;
  149. }
  150.  
  151. public void start() {
  152.   if(timer == null)
  153.     {
  154.       timer = new Thread(this);
  155.       timer.start();
  156.     }
  157. }
  158.  
  159. public void stop() {
  160.   timer = null;
  161. }
  162.  
  163. public void run() {
  164.   while (timer != null) {
  165.     try {Thread.sleep(100);} catch (InterruptedException e){}
  166.     repaint();
  167.   }
  168.   timer = null;
  169. }
  170.  
  171. public void update(Graphics g) {
  172.   paint(g);
  173. }
  174. }
  175.  
  176.